<script setup name="Dialog">
import {onMounted} from "vue";
import {baseStore} from "@/store/baseStore.js";
import json from "./dialogJson.js";
import ZIndex from "@/components/dialog/zIndex.js";


let bs = baseStore()
let {menuFoldState} = storeToRefs(bs)
const closeTag = ref(true)
const timer = ref(null)

const props = defineProps({
    dialog: {
        type: Object,
        default: () => {
            return json
        }
    }
})
const emits = defineEmits([
    'close'
])
// 内部事件执行成功才能让去关闭
const closeReturn = (value) => {
    closeTag.value = value ? value : false
}
const close = () => {
    emits('close')
    timer.value = setInterval(() => {
        if(!closeTag.value){
            clearInterval(timer.value)
            return
        }
        props.dialog.show = false
        ZIndex.subtract()
        clearInterval(timer.value)
    },100)
}

defineExpose({
    closeReturn,
})
</script>
<template>
    <el-row :class="{'dialog-modal': true,'dialog-frame':dialog.frame,'dialog-menu-fold': menuFoldState }"
            v-if="dialog.show" :style="`z-index: ${ZIndex.add()}`">
        <el-row class="dialog-row" :style="{'width':dialog.width }">
            <el-row class="dialog-row-from">
                <el-row class="dialog-row-head">
                    <el-row class="dialog-row-head-title">
                        {{ dialog.title }}
                    </el-row>
                    <div class="flex-grow"/>
                    <el-row class="dialog-row-head-close" @click="close" v-if="dialog.close">
                        <el-icon style="margin: auto">
                            <Close/>
                        </el-icon>
                    </el-row>
                </el-row>
                <el-row style="width: 100%;" v-loading="dialog.load">
                    <div class="dialog-row-content" v-if="!dialog.frame">
                        <slot></slot>
                    </div>
                    <slot v-else></slot>
                    <el-row class="dialog-row-foot">
                        <slot name="table-dialog-btn"></slot>
                    </el-row>
                </el-row>
            </el-row>
        </el-row>
    </el-row>
</template>

<style lang="scss" scoped>
.dialog-menu-fold {
    left: 63px !important;
    width: calc(100% - 63px) !important;
    transition-property: left, width;
    transition-duration: .4s;
    transition-timing-function: ease;
}

.dialog-frame {
    width: 100% !important;
    height: calc(100vh - 59px);
    left: 0px !important;
}

.dialog-modal {
    width: calc(100% - 200px);
    height: calc(100vh - 59px);
    top: 59px;
    left: 200px;
    position: absolute;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 10;

    .dialog-row {
        margin: 100px auto auto auto;
    }

    .dialog-row-from {
        width: 100%;
        border-radius: 3px;
        padding: 0 20px;
        --el-dialog-bg-color: var(--el-bg-color);
        background: var(--el-dialog-bg-color);
    }

    .dialog-row-head {
        width: 100%;
        height: 60px;
        line-height: 60px;
    }

    .dialog-row-head-title {
        font-size: 18px;
        font-weight: 549;
    }

    .dialog-row-head-close {
        height: 60px;
        width: 20px;
    }

    .dialog-row-content {
        min-height: auto;
        max-height: calc(100vh / 2);
        overflow-y: auto;
        width: 100%;
    }

    .dialog-row-foot {
        padding: 20px 15px;
        width: 100%;
        display: flex;
        justify-content: end;
    }
}

.flex-grow {
    flex-grow: 1;
}

.dialog-row-head-close:hover {
    cursor: pointer;
}
</style>